<?php
/**
 * Created by PhpStorm.
 * User: luxuewen_gz
 * Mail: lu.xuewen@qq.com
 * Date: 2018/12/18
 * Time: 9:29
 * //
 * //                            _ooOoo_
 * //                           o8888888o
 * //                           88" . "88
 * //                           (| -_- |)
 * //                           O\  =  /O
 * //                        ____/`---'\____
 * //                      .'  \\|     |//  `.
 * //                     /  \\|||  :  |||//  \
 * //                    /  _||||| -:- |||||-  \
 * //                    |   | \\\  -  /// |   |
 * //                    | \_|  ''\---/''  |   |
 * //                    \  .-\__  `-`  ___/-. /
 * //                  ___`. .'  /--.--\  `. . __
 * //               ."" '<  `.___\_<|>_/___.'  >'"".
 * //              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //              \  \ `-.   \_ __\ /__ _/   .-` /  /
 * //         ======`-.____`-.___\_____/___.-`____.-'======
 * //                            `=---='
 * //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * //                      佛祖保佑       永无BUG
 */

namespace app\admin\logic;

use \app\admin\validate\Login as LoginValidate;
use think\Session;

class Login
{
    public function loginSubmit($data){
        $validate = new LoginValidate;
        $result = $validate->check($data);
        if(!$result){
            return [
                'bool'      =>  false,
                'msg'       =>  $validate->getError()
            ];
        }
        $where[] = ['is_delete', '=', 0];
        $where[] = ['username', 'eq', $data['username']];
        $where[] = ['password', 'eq',  md5($data['password'])];
        $res = model('User')->login($where);
        if(!$res){
            return [
                'bool'      =>  false,
                'msg'       =>  '用户名或密码错误，请重新输入'
            ];
        }
        $SESSION = new Session;
        $SESSION->set('USER_INFO.userId', $res['id']);
        $SESSION->set('USER_INFO.userName', $data['username']);
        return [
            'bool'      =>  true,
            'msg'       =>  '登录成功'
        ];
    }
    public function loginCheck(){
        return (new Session())->has('USER_INFO') ? true : false;
    }
    public function loginOut(){
        return (new Session())->clear();
    }

}